package org.redmaple.core.pkstrategy;

import java.util.UUID;

/**
 * UUID主键生成策略
 * 
 * @author Xiangyun
 */
public class UUIDGeneratedStrategy extends AbstarctStringGeneratedStrategy {

	/**
	 * 系统默认的分隔符
	 */
	private static final String DEFAULT_SEPARATOR = "-";

	/**
	 * 自定义的分隔符,默认为空字符串
	 */
	private String separator;

	/**
	 * 自定义的前缀,默认为空字符串
	 */
	private AbstarctStringGeneratedStrategy prefix;

	/**
	 * 自定义的后缀,默认为空字符串
	 */
	private AbstarctStringGeneratedStrategy suffix;

	public UUIDGeneratedStrategy(String separator,
			AbstarctStringGeneratedStrategy prefix,
			AbstarctStringGeneratedStrategy suffix) {
		this.separator = separator;
		this.prefix = prefix;
		this.suffix = suffix;
	}

	public UUIDGeneratedStrategy(AbstarctStringGeneratedStrategy prefix,
			AbstarctStringGeneratedStrategy suffix) {
		this.prefix = prefix;
		this.suffix = suffix;
	}

	public UUIDGeneratedStrategy(String separator) {
		this.separator = separator;
		this.prefix = new StringGeneratedStrategy();
		this.suffix = new StringGeneratedStrategy();
	}

	public UUIDGeneratedStrategy() {
		this.prefix = new StringGeneratedStrategy();
		this.suffix = new StringGeneratedStrategy();
	}

	public String getValue() {
		String uuidValue = UUID.randomUUID().toString()
				.replaceAll(DEFAULT_SEPARATOR, separator);
		StringBuilder buffer = new StringBuilder();
		if (null != prefix) {
			buffer.append(prefix.getValue());
		}
		buffer.append(uuidValue);
		if (null != suffix) {
			buffer.append(suffix.getValue());
		}
		return buffer.toString();
	}

}
